package com.eclink.asset.portal.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eclink.asset.portal.domain.dao.PortalDataBoard;
import com.eclink.asset.portal.domain.dao.PortalDataBoardGroup;
import com.eclink.asset.portal.domain.dao.SysUserDataBoard;
import com.eclink.asset.portal.domain.dto.PortalDataBoardDTO;
import com.eclink.asset.portal.domain.dto.PortalDataBoardSettingDTO;
import com.eclink.asset.portal.domain.dto.PortalDataBoardGroupDTO;
import com.eclink.asset.portal.mapper.PortalDataBoardGroupMapper;
import com.eclink.asset.portal.mapper.PortalDataBoardMapper;
import com.eclink.asset.portal.mapper.SysUserDataBoardMapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DataBoardService {

    @Resource
    private PortalDataBoardGroupMapper portalDataBoardGroupMapper;

    @Resource
    private PortalDataBoardMapper portalDataBoardMapper;

    @Resource
    private SysUserDataBoardMapper sysUserDataBoardMapper;

    public List<PortalDataBoardGroup> list(int boardType) {
        List<PortalDataBoardGroup> groupList = portalDataBoardGroupMapper.selectList(new QueryWrapper<>());
        for(PortalDataBoardGroup group : groupList) {
            QueryWrapper<PortalDataBoard> qw = new QueryWrapper<>();
            qw.eq("group_id", group.getId());
            qw.eq("board_type", boardType);
            qw.eq("enable", 1);
            group.setChildren(portalDataBoardMapper.selectList(qw));
        }
        return groupList;
    }

    public List<Long> getCommonBoardIdList() {
        QueryWrapper<PortalDataBoard> qw = new QueryWrapper<>();
        qw.eq("enable", 1);
        qw.eq("common_flag", 1);
        List<PortalDataBoard> list = portalDataBoardMapper.selectList(qw);
        return list.stream().map(PortalDataBoard::getId).collect(Collectors.toList());
    }

    public List<Long> getUserBoardIdList() {
        QueryWrapper<SysUserDataBoard> qw = new QueryWrapper<>();
        qw.eq("user_id", SecurityUtils.getUserId());
        List<SysUserDataBoard> list = sysUserDataBoardMapper.selectList(qw);
        if (list == null || list.isEmpty()) {
            return getCommonBoardIdList();
        }
        return list.stream().map(SysUserDataBoard::getBoardId).collect(Collectors.toList());
    }

    public List<PortalDataBoard> getCommonBoardList() {
        QueryWrapper<PortalDataBoard> qw = new QueryWrapper<>();
        qw.eq("enable", 1);
        qw.eq("common_flag", 1);
        return portalDataBoardMapper.selectList(qw);
    }

    public List<PortalDataBoard> getMyBoardList() {
        MPJLambdaWrapper<PortalDataBoard> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(PortalDataBoard.class).select(SysUserDataBoard::getUserId)
                .innerJoin(SysUserDataBoard.class, SysUserDataBoard::getBoardId, PortalDataBoard::getId);
        wrapper.eq(SysUserDataBoard::getUserId, SecurityUtils.getUserId());
        List<PortalDataBoard> myDataBoardList = portalDataBoardMapper.selectJoinList(PortalDataBoard.class, wrapper);
        if (myDataBoardList.isEmpty()) {
            return getCommonBoardList();
        }
        return myDataBoardList;
    }

    public List<PortalDataBoardGroupDTO> selectMyDataBoardGroup(int boardType) {
        List<PortalDataBoardGroup> list = list(boardType);
        List<PortalDataBoardGroupDTO> dtoList = new ArrayList<>();
        List<Long> userBoardIdList = getUserBoardIdList();
        for(PortalDataBoardGroup group : list) {
            PortalDataBoardGroupDTO dto = new PortalDataBoardGroupDTO();
            BeanUtil.copyProperties(group, dto);
            dtoList.add(dto);
            List<PortalDataBoard> children = group.getChildren();
            List<PortalDataBoardSettingDTO> newChildren = new ArrayList<>();
            for(PortalDataBoard child : children) {
                PortalDataBoardSettingDTO dtoChild = new PortalDataBoardSettingDTO();
                BeanUtil.copyProperties(child, dtoChild);
                if (userBoardIdList.contains(child.getId())) {
                    dtoChild.setChecked(true);
                }
                newChildren.add(dtoChild);
            }
            dto.setChildren(newChildren);
        }
        return dtoList;
    }

    /**
     * 保存数据看板的设置
     */
    public void saveMyDataBoard(List<Long> dataBoardIds) {
        QueryWrapper<SysUserDataBoard> qw = new QueryWrapper<>();
        Long userId = SecurityUtils.getUserId();
        qw.eq("user_id", userId);
        sysUserDataBoardMapper.delete(qw);
        for(Long boardId : dataBoardIds) {
            SysUserDataBoard userFavoritesMenu = new SysUserDataBoard();
            userFavoritesMenu.setBoardId(boardId);
            userFavoritesMenu.setUserId(userId);
            sysUserDataBoardMapper.insert(userFavoritesMenu);
        }
    }

}
